<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="角色名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="findList">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="addCompany">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table
      :data="list"
      highlight-current-row
      v-loading="listLoading"
      style="width: 100%;"
      height="70vh"
    >
      <el-table-column prop="name" label="角色名称" width="120" align="center"></el-table-column>
      <el-table-column prop="description" label="描述" align="center"></el-table-column>
      <el-table-column prop="sort" label="序号" align="center"></el-table-column>
      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="small" @click="handEdit(scope.row)">编辑</el-button>
          <el-button size="small" @click="configure(scope.row)">权限配置</el-button>
          <el-button size="small" @click="handDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-col :span="24" class="toolbar">
      <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->
      <el-pagination
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :page-size="pageSize"
        :total="total"
        style="float:right;"
        :current-page="page"
      ></el-pagination>
    </el-col>
    <!--新增 编辑界面-->
    <el-dialog title="角色" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <addRole @hideTotas="hideTotas" v-if="addFormVisible" :sendFormData="sendFormData"></addRole>
    </el-dialog>
    <!-- 权限配置 -->
    <el-dialog title="权限配置" :visible.sync="powerVisible" :close-on-click-modal="false">
      <el-form :model="form" :inline="true" v-loading="loadRoleTotas">
        <el-form-item label="分配菜单">
          <!-- <el-select v-model="form.menuIds" multiple placeholder="请选择" value-key="menuId">
            <el-option
              v-for="(item,index) in options"
              :key="index"
              :label="item.name"
              :value="item.menuId"
            ></el-option>
          </el-select>-->
          <el-cascader
            :options="options"
            v-model="form.menuIds"
            :props="{ multiple: true, checkStrictly: true,label:'name',value:'menuId',children:'sonMenu' }"
            :show-all-levels="false"
            clearable
            collapse-tags
          ></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="allSelect">全选</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="hideRoleMenu" :disabled="loadRoleTotas">取 消</el-button>
        <el-button type="primary" @click="addRoleMenu" :loading="loadRoleTotas">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import addRole from "./components/addRole";
import {
  companyList,
  getRoleList,
  delRole,
  // menuList,
  setAuthority,
  getMenuList,
  menuSelectMenuAllTreeList
} from "../../api/baseSetting";

export default {
  data() {
    return {
      filters: {
        name: ""
      },
      //权限配置
      form: {
        menuIds: [],
        roleId: ""
      },
      options: [], //选择路由
      addFormVisible: false,
      powerVisible: false, //权限配置弹出
      list: [],
      total: 0,
      page: 1,
      pageSize: 10,
      listLoading: false,
      sendFormData: {
        //点中的公司参数
      },
      loadRoleTotas: false
    };
  },
  components: {
    addRole
  },
  methods: {
    //点击全选
    allSelect() {
      // let arr = [];
      // for (let i = 0; i < this.options.length; i++) {
      //   arr.push(this.options[i].menuId);
      // }
      // this.form.menuIds = arr;
      // this.form.menuIds = [];
      let list = this.options;
      let arr = this.formatArr(list);
      this.form.menuIds = arr;
    },
    //格式化成显示的数据
    formatArr(list) {
      let newArr = [];
      for (let i = 0; i < list.length; i++) {
        let arr = [];
        arr.push(list[i].menuId);
        let joinArr = [...arr];
        newArr.push(joinArr);
        if (list[i].sonMenu && list[i].sonMenu.length > 0) {
          function getMenuIdArr(dataList,centerArr) {
            for (let a = 0; a < dataList.length; a++) {
              let newList = [...centerArr,dataList[a].menuId]
              // arr.push(dataList[a].menuId);
              newArr.push(newList);
              if (dataList[a].sonMenu && dataList[a].sonMenu.length > 0) {
                getMenuIdArr(dataList[a].sonMenu,newList);
              }
            }
          }
          getMenuIdArr(list[i].sonMenu,arr)
        }
      }
      return newArr;
    },
    //点击删除
    handDelete(e) {
      let roleId = e.roleId;
      this.$confirm("确认删除该角色", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delRole({ roleId }).then(res => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.filters.name = "";
            this.page = 1;
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "fail",
            message: "删除失败!"
          });
          this.$store.commit("setCompanyList", []);
          this.page = 1;
          this.filters.companyName = "";
          this.getList();
        });
    },
    //点击编辑
    handEdit(detail) {
      this.sendFormData = detail;
      this.addFormVisible = true;
    },
    //点击权限分配
    configure(e) {
      this.powerVisible = true;
      this.form.roleId = e.roleId;
      this.getUserMenu(e.roleId);
    },
    //获取角色权限
    getUserMenu(roleId) {
      getMenuList([roleId]).then(res => {
        if (res) {
          // let arr = [];
          // for (let i = 0; i < res.length; i++) {
          //   arr.push(res[i].menuId);
          // }
          // this.form.menuIds = arr;
          let arr = this.formatArr(res);
          console.log(arr);
          this.form.menuIds = arr;
        }
      });
    },
    //分页
    handleCurrentChange(val) {
      this.page = val;
      this.getList();
    },
    //隐藏新增表单
    hideTotas() {
      this.addFormVisible = false;
      this.filters.companyName = "";
      this.page = 1;
      this.getList();
    },
    //点击新增
    addCompany() {
      this.sendFormData = {};
      this.addFormVisible = true;
    },
    //点击查询
    findList() {
      this.page = 1;
      this.getList();
    },
    //获取角色列表
    getList() {
      let para = {
        name: this.filters.name,
        page: {
          page: this.page,
          pageSize: this.pageSize
        }
      };
      this.listLoading = true;
      getRoleList(para)
        .then(res => {
          this.listLoading = false;
          this.list = res.records;
          this.total = res.total;
        })
        .catch(err => {
          this.listLoading = false;
        });
    },
    //获取菜单列表  用于分配权限
    getMenu() {
      menuSelectMenuAllTreeList({}).then(res => {
        this.options = res;
      });
    },
    //隐藏分配权限弹出
    hideRoleMenu() {
      this.powerVisible = false;
      this.form.menuIds = [];
    },
    //点击确认 角色分配菜单
    addRoleMenu() {
      // console.log(this.form.menuIds);
      // return;
      let array = [];
      let menuIds = this.form.menuIds;
      for (let i = 0; i < menuIds.length; i++) {
        let num = menuIds[i].length;
        let menuId = menuIds[i][num - 1];
        array.push(menuId);
      }
      let sendData = {
        roleId: this.form.roleId,
        menuIds: array
      };
      this.loadRoleTotas = true;
      setAuthority(sendData)
        .then(res => {
          this.loadRoleTotas = false;
          this.$message({
            type: "success",
            message: "成功"
          });

          this.page = 1;
          this.form = {
            menuIds: [],
            roleId: ""
          };
          this.hideRoleMenu();
          this.getList();
        })
        .catch(err => {
          this.loadRoleTotas = false;
        });
    }
  },
  mounted() {
    this.getList();
    this.getMenu();
  }
};
</script>
<style lang="less" scoped>
</style>
